<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redis;

/**
 * App\Models\SystemConfig
 *
 * @property int $id
 * @property string $conf_key 配置项键名
 * @property string $conf_val 配置项键值
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 修改时间
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SystemConfig whereConfKey($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SystemConfig whereConfVal($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SystemConfig whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SystemConfig whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SystemConfig whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class SystemConfig extends Model
{
    public $guarded = [];
    public $table = 'system_config';
    public $kye_list = [
        'recharge_money', //充值优惠满多少金额
        'gift_money',     //充值满足recharge_money数量后赠送多少金额
        'recharge_limit', //充值优惠次数限制
        'gift_limit',     //赠送优惠上限
        'cash_min_money', //提现需要满足的最低金额
        'cash_service_charge', //提现手续费
        'red_bag_total_amount', #红包限定金额
        'vip_limit'           , #会员卡临界金额
        'commission_rate'       #
    ];

    public static function getConfValByConfKey(string $key)
    {
        $model = self::where('conf_key',$key)->first();
        $conf  =  empty($model)?[]:$model->toArray();
        return $conf;
    }

    public static function modifyValByConfKey(string $key,string $val)
    {
        if(!self::getConfValByConfKey($key)){
            $flag = self::create(['conf_key'=>$key,'conf_val'=>$val]) == true;
        }else{
            $flag       =  self::where('conf_key',$key)->update(['conf_val'=>$val]);
        }
        if ($flag > 0){
            return true;
        }else{
            return false;
        }
    }




}
